<template>
	<view class="container">
		<view class="tips">我的邀请码</view>
		<el-input v-model="inviteCode" placeholder="邀请码" disabled style="width: 100px;margin-top: 3px;"></el-input>
		<view class="item">
		  <el-button @click="generateInviteCode">生成邀请码</el-button>
		</view>
		<view class="item">
			<el-button @click="copyInviteCode">复制邀请码</el-button>
		</view>
		<view class="item">
			<el-button @click="saveInviteCode">保存我的邀请码</el-button>
		</view>
		<view class="tips">填写邀请码</view>
		<el-input v-model="joinInviteCode" placeholder="邀请码" style="width: 110px;"></el-input>
		<view class="item">
			<el-button @click="verfiyInviteCode">邀请验证</el-button>
		</view>
	</view>
</template>

<script>
	var http = require("../../utils/http.js");
	var util = require("../../utils/util.js");
	var config = require("../../utils/config.js");
export default {
  data() {
    return {
      inviteCode: '',
	  joinInviteCode:''
    };
  },
  methods: {
		generateRandomString(length) {
		  const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		  let result = '';
		  do {
			result = '';
			for (let i = 0; i < length; i++) {
			  result += chars.charAt(Math.floor(Math.random() * chars.length));
			}
		  } while (this.checkDuplicateInviteCode(result)); // 检查生成的邀请码是否重复
		  return result;
		},
		checkDuplicateInviteCode(inviteCode) {
			var params = {
			url: "/p/user/checkDuplicateInviteCode",
			method: "get",
				data: {
				  inviteCode: inviteCode
				},
				callBack: function (res) {
					console.log("校验结果：",res)
					// if(res.success == 'true'){
						return true;
					// }
				}
			}
			http.request(params)
		  return false; // 假设默认不重复
		},
		generateInviteCode() {
		  this.inviteCode = this.generateRandomString(8);
		},
		copyInviteCode() {
		  if (this.inviteCode === '') {
			uni.showToast({
			  icon: "none",
			  title: '请先生成邀请码'
			});
		  } else {
			navigator.clipboard.writeText(this.inviteCode);
			uni.showToast({
			  icon: "none",
			  title: '邀请码已复制'
			});
		  }
		},
		saveInviteCode() {
		  if (this.inviteCode === '') {
			uni.showToast({
			  icon: "none",
			  title: '请先生成邀请码'
			});
		  } else {
			// 向后台发送保存邀请码信息的逻辑
			  var params = {
				url: "/p/user/setInviteCode",
				method: "get",
					data: {
					  inviteCode: this.inviteCode
					},
					callBack: res => {
						uni.showToast({
						  icon: "none",
						  title: '保存成功'
						});
					}
				}
				uni.hideLoading();
				http.request(params)
		  }
		},
		queryUserInviteCode(){
			var params = {
						url: "/p/user/queryUserInviteCode",
						method: "get",
							callBack: res => {
								this.setData({
								  inviteCode: res
								});
							}
						}
			uni.hideLoading();
			http.request(params)
			},
		verfiyInviteCode(){
			if (this.joinInviteCode === '') {
				uni.showToast({
				  icon: "none",
				  title: '请填写邀请码'
				});
			} else {
			// 向后台发送保存邀请码信息的逻辑
			  var params = {
				url: "/p/invite/joinInvite",
				method: "get",
					data: {
					  inviteCode: this.joinInviteCode
					},
					callBack: res => {
							uni.showToast({
							  icon: "none",
							  title: '邀请成功'
							});
						
					}
				}
				uni.hideLoading();
				http.request(params)
				this.joinInviteCode = ''
			}
		}
		},
		created() {
			this.queryUserInviteCode()
		}
};
</script>

<style>
	@import "../../pages/pay-result/pay-result.css";
.container {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.item {
  margin: 5px; /* 设置组件之间的间距 */
}
</style>
